package webapp;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
//import java.sql.ResultSet;
//import java.sql.Statement;




import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mysql.jdbc.PreparedStatement;

/**
 * Servlet implementation class Register
 */
public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Register() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("We are in Service method of Register");
		PrintWriter out=response.getWriter(); 
		HttpSession session=request.getSession(false); 
		String un = (String) session.getAttribute("name");
		
        if(un!=null){
		
		String nm=request.getParameter("name");
		String em=request.getParameter("email");
		String un1=request.getParameter("username");
		String pw=request.getParameter("password");
		
		String msg=" ";
		
		 
		String url = "jdbc:mysql://localhost:3306/";
		String dbName = "webappschema";
		String driver = "com.mysql.jdbc.Driver";
		String dbusername = "root";
		String dbpassword = "webapp";
		 
		 if(nm.equals("") || em.equals("") || un1.equals("") || pw.equals("")) {
			 
			 msg = "Please insert valid data";
			     
			 response.setContentType("text/html");
			
			 out.println("<font size = '6' color=red>" + msg + "</font>");
			}
		 else {
		     try {
		    	 	Class.forName(driver).newInstance();
		    	 	Connection con = DriverManager.getConnection(url+dbName,dbusername,dbpassword);
		    	 	String getEmail = "SELECT * from login where Email= '" + em + "' " ;
		    	 	Statement st_email = con.createStatement();
		            ResultSet rs = st_email.executeQuery(getEmail);
		            
		           if (rs.next()) {
		            	
		            	msg = "This email is already registered";
		                
		                response.setContentType("text/html");
		      			
		      			 out.println("<font size = '6' color=red>" + msg + "</font>");
		            	
		            }	
		           else  {
			            st_email.close();
			    	 	String strQuery = ("INSERT INTO login (Name,Email,UserName,Password) VALUES (?,?,?,?)");
				 
				 
			    	 	PreparedStatement ps = (PreparedStatement) con.prepareStatement(strQuery);
				 
			    	 	ps.setString(1, nm);
			    	 	ps.setString(2, em);
			    	 	ps.setString(3, un1);
			    	 	ps.setString(4, pw);
			     
			    	 	int i=ps.executeUpdate();
			
			    	 	if(i>0){
			    	 		
			    	 		out.println("<script type=\"text/javascript\">");
			    	 	   out.println("alert('User successfully registered');");
			    	 	   out.println("location='Register.jsp';");
			    	 	   out.println("</script>");
			         
			    	 		//out.print("User successfully registered");    
				            //request.getRequestDispatcher("register.jsp").include(request, response);  
				        } 
				        
			           
			    	 	
			    	 	ps.close(); 
			    	 	con.close();
			            }
		     } catch (Exception e) {
		    	 e.printStackTrace();
		     }
	 
		     
			 }
		 }
		 else{  
				out.print("Please login first");    
	            request.getRequestDispatcher("index.jsp").include(request, response);  
	        } 
	        out.close();
		}
	
}